<%@include file="/header.jsp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 
<%@ page import="java.util.*" %>   
<%@ page import="domainLogic.ItemView" %>
<%
	final int MAXROW=10;             //items per page
	int totalrows;  				//total pages
	int start;                      //start no.
	int end;   						//end no.
	int totalpage;					//total pages
	int current;					//current page
	int next;						//next page
	int prev;						//previous page

	//read page no. tranfered
	if(request.getParameter("go") == null)
		current = 1;
	else 
		current = Integer.parseInt(request.getParameter("go"));
	
	next = current + 1;
	prev = current - 1;
	
	//--------------------------------------------------------------------
	//get a username from the session
	String userId = (String) session.getAttribute("username");

	//search username & keyword
	String seller = request.getParameter("seller");
	if (seller == null) 
		seller = "";
	seller = seller.toLowerCase();
	
	String keyword  = request.getParameter("keyword");
	if (keyword == null) 
		keyword = ""; 
	keyword = keyword.toLowerCase();
	
	int len = 0;
	List<ItemView> list = null;
	
	String colSellerWinner = "Seller";
	String colTime = "Ended date";
	String prtEmptyList =  "No searched auction items!!!";
	
	// List Type 0:Current bids, 1:Completed, won bids, 2:Completed, sold bids
	int listtype = 0;
	
	String listtypeS = request.getParameter("listtype");
	if(listtypeS != null)
		listtype = Integer.parseInt(listtypeS);
	
	// sort type : price / time
	String sorttype = request.getParameter("sorttype");
	boolean isOrderByPrice = true;
	
	if(sorttype != null && sorttype.equals("time"))
	{
		isOrderByPrice=false;
	}
	else
	{
		sorttype = "price";		
	}
	
	
	switch(listtype)
	{
	case 1:	// completed won bids
		len = dataGateway.ItemViewTable.getInstance().getNumItemView(
				true, seller, keyword, userId);
		colTime = "Ended date";
		colSellerWinner = "Seller";
		prtEmptyList = "No completed bidding items you won!!!";
		break;
	case 2: // completed sold bids
		len = dataGateway.ItemViewTable.getInstance().getNumItemView(
				true, userId, keyword, null);
		colTime = "Ended date";
		colSellerWinner = "Winner";
		prtEmptyList = "No completed bidding items you sold!!!";
		break;
	case 0:	// current bids
	default:
		len = dataGateway.ItemViewTable.getInstance().getNumItemView(
				false, seller, keyword, null);		
		colTime = "Remaining time";
		colSellerWinner = "Seller";
		prtEmptyList = "No searched auction items!!!";
		break;
	}

	final String linkListPage = common.JspUtil.URL_CONTENT_PAGE + "/items/viewItemList.jsp";
	final String linkListPageWithoutSort = linkListPage + "?seller="+ seller
			+ "&keyword="+keyword +"&listtype="+listtype;	
	final String linkListPageParam = linkListPageWithoutSort+"&sorttype="+sorttype;
%>
	
	<!-- --------------------------------------------------------------------- -->
	<form method="get" name="searchForm">
	<input type="hidden" name="listtype" value="<%=listtype%>">
	<input type="hidden" name="sorttype" value="<%=sorttype%>">
		<div>
			<table border="0" align="center">
				<tr><td align="right">
			<%if(listtype !=2){%>seller:&nbsp;<input type="text" name="seller" value="<%=seller%>">&nbsp;<%} %>
				keyword:&nbsp;<input type="text" name="keyword" value="<%=keyword%>">&nbsp;
				<input type="submit" value="Search">
				</td></tr>
			</table>
		</div>
	</form>
		<table border="1" align="center">
				<tr>
					<th bgcolor="#AFCCCE">Item</th>
					<th bgcolor="#AFCCCE"><%=colSellerWinner%></th>					
					<th bgcolor="#AFCCCE"># Bids</th>
					<th bgcolor="#AFCCCE"><a href="<%=linkListPageWithoutSort%>&sorttype=price">Price</a></th>					
					<th bgcolor="#AFCCCE">Buyout Price</th>
					<th bgcolor="#AFCCCE"><a href="<%=linkListPageWithoutSort%>&sorttype=time"><%=colTime %></a></th>		
				</tr>
<%
	if (len > 0) {
		totalrows = len;
		totalpage = (int)Math.ceil((double)totalrows/MAXROW);   //calculate total pages
		start = (current-1)*MAXROW;          //start
		
		switch(listtype)
		{
		case 1:	// completed won bids
			list = dataGateway.ItemViewTable.getInstance().getItemView(
					true, seller, keyword, userId, isOrderByPrice, start, MAXROW );
			break;
		case 2: // completed sold bids
			list = dataGateway.ItemViewTable.getInstance().getItemView(
					true, userId, keyword, null, isOrderByPrice, start, MAXROW);
			break;
		case 0:	// current bids
		default:
			list = dataGateway.ItemViewTable.getInstance().getItemView(
					false, seller, keyword, null, isOrderByPrice, start, MAXROW );
			break;
		}
	
		for(ItemView dao: list)
		{
%>				
			<tr>	
				<td align="center"><a href="<%=common.JspUtil.URL_CONTENT_PAGE%>/items/viewItemDetail.jsp?itemId=<%=dao.getId()%>"><%=dao.getName() %></a></td>
				<td align="center">
				<%
				if(colSellerWinner.equals("Winner"))
					out.print(dao.getWinner());
				else
					out.print(dao.getUserId()); 
				%>
				</td>		
				<td align="center"><%=dao.getNumBids() %></td>
				<td align="center">$ <%=dao.getPrice()%></td>				
				<td align="center">$ <%=dao.getBuyoutprice() %></td>
				<td align="center">
				<%
				if(colTime.equals("Remaining time"))
					out.print(dao.getRemainingTime());
				else
					out.print((dao.getRealenddate().toString()).substring(0,16)); 
				%>
				</td>
			</tr>
<%
		}
%>
<!-- --------------------------------------------------------------------- -->
			<tr><td colspan="8" align="center">
<!-- First , Previuos page------------------------------------------------ -->
<%
		if(current == 1)
		{
%>
			<font color="red" size="2">[First]&nbsp;[Prev]</font>
<%		
		} else {
%>
			<font color="red" size="2">[
			<a href="<%=linkListPageParam%>&go=1">Frist</a>]
			&nbsp;[
			<a href="<%=linkListPageParam%>&go=<%=prev%>">Prev</a>]</font>
<%		
		}	
%>
<!-- Next , Lst page------------------------------------------------------ -->
<%
		if(current == totalpage)
		{
%>
			<font color="red" size="2">[Next]&nbsp;[Last]</font>
<%		
		} else {
%>
			<font color="red" size="2">[
			<a href="<%=linkListPageParam%>&go=<%=next%>">Next</a>]
			&nbsp;[
			<a href="<%=linkListPageParam%>&go=<%=totalpage%>">Last</a>]</font>
<%		
		}	
%>
		<br>
		Total: <%=len%>
		</td></tr>
<%
	}
	else {
%>
		<tr><td colspan="8" align="center" height="100">
			<font color="red" size="3"><%=prtEmptyList %></font>
		</td></tr>
<% } %>
	</table>
<!-- -------------------------------------------------------------------- -->			
<%@include file="/footer.jsp"%>